//
//  ViewController.m
//  3-10
//
//  Created by mac on 16-3-10.
//  Copyright (c) 2016年 ZF. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()


- (IBAction)eat:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *niian;
- (IBAction)drink:(id)sender;
- (IBAction)knockout:(id)sender;
- (IBAction)cymbal:(id)sender;
- (IBAction)fart:(id)sender;
@end

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)startAnimating:(int)count picName:(NSString*)picName
{
    //1.加载图片到帧动画数组中去
    NSMutableArray *arrayM = [NSMutableArray array];
    for (int i = 0; i<count; i++) {
        //拼接图片名字
        NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg",picName,i];
        //根据图片文字得到图片
//       UIImage *imgCat = [UIImage imageNamed:imageName];
        NSString * path =[[NSBundle mainBundle]pathForResource:imageName ofType:nil];
        
        UIImage * imgCat = [UIImage imageWithContentsOfFile:path];
        //将每次获取的图片放到数组中去
        [arrayM addObject:imgCat];
    }
    //2.将得到的帧动画数组赋给UIImageView的帧动画数组属性
    self.niian.animationImages = arrayM;
    
    //3.设置动画时间
    self.niian.animationDuration = self.niian.animationImages.count*0.1;
    
    //4.设置动画循环次数
    self.niian.animationRepeatCount = 1;
    
    //5.开始播放动画
    [self.niian startAnimating];
    //.6动画播放完之后清空缓存
//    self.niian.animationImages = nil;
    [self.niian performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.niian.animationImages.count * 0.1];
}

- (IBAction)eat:(id)sender
{
    [self startAnimating:39 picName:@"eat"];
}

- (IBAction)cymbal:(id)sender {
[self startAnimating:12 picName:@"cymbal"];}

- (IBAction)fart:(id)sender {    [self startAnimating:27 picName:@"fart"];
}
- (IBAction)drink:(id)sender {
    [self startAnimating:80 picName:@"drink"];
}
- (IBAction)knockout:(id)sender
{
    [self startAnimating:80 picName:@"knockout"];
}
@end
